package v1

import (
	"usplatformserver/global"
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

// @Summer 登录验证
// @Produce json
// @Param userName query string true "用户名" maxlength(100)
// @Param userPassword query string true "用户密码" maxlength(100)
// @Success 200 {object} model.TokenInfoSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/login [post]
func GetUser(c *gin.Context) {
	param := service.LoginRequest{}
	response := app.NewResponse(c)

	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	svc := service.New(c.Request.Context())
	_, err := svc.GetUserByName(param.UserName)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorUserNotExist)
		return
	}

	id, err := svc.GetUser(param.UserName, param.UserPassword)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorUserPasswordError)
		return
	}

	token, err := app.GenerateToken(id)
	if err != nil {
		global.Logger.Errorf("app.GenerateToken err: %v", err)
		response.ToErrorResponse(errcode.UnauthorizedTokenGenerate)
		return
	}

	response.ToResponse(gin.H{
		"token": token,
	})
}

// @Summer 登出
// @Produce json
// @Param token header string true "token"
// @Success 200 {object} model.SwaggerSuccess "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/logout [post]
func Logout(c *gin.Context) {
	response := app.NewResponse(c)
	response.ToResponseSuccess()
}

// @Summer 获取版本号
// @Produce json
// @Success 200 {object} model.VersionInfoSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/app/version [get]
func GetVersion(c *gin.Context) {
	response := app.NewResponse(c)
	response.ToResponse(gin.H{
		"version": app.AppVersion,
	})
}
